Interactive Map of Michigan

Author

Ella Louise Stern

Published

April 30, 2023

Interactive Maps with MapView Package

A h/t to Sharon Machlis for turning people on to the Mapview package, and her excellent walkthroughs of it.

For the data tonight we’ll be using the tidycensus package to pull both census data, as well as geospatial boundaries. Let’s quickly review how we use it.

# uncomment to run, then recomment it out so you don't run it every time

census_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", install=TRUE, overwrite=TRUE)
Your original .Renviron will be backed up and stored in your R HOME directory if needed.
Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). 
To use now, restart R or run `readRenviron("~/.Renviron")`
[1] "2a6f8c21a30d3024e038d67d7d4eba647dc79cd4"
readRenviron("~/.Renviron")
#chose variables we want
myvars <- c(totalpop = "B01003_001",
            medincome = "B19013_001",
            medage = "B01002_001"
)
#pull for MI counties
 
mi_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       state = "MI",
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
#all counties in the US
all_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
all_counties_withgeo
Simple feature collection with 3221 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -179.1489 ymin: 17.88328 xmax: 179.7785 ymax: 71.36516
Geodetic CRS:  NAD83
First 10 features:
   GEOID                              NAME totalpopE totalpopM medincomeE
1  20161              Riley County, Kansas     72602        NA      53296
2  19159             Ringgold County, Iowa      4739        NA      57700
3  30009            Carbon County, Montana     10488        NA      63178
4  16007           Bear Lake County, Idaho      6327        NA      60337
5  55011         Buffalo County, Wisconsin     13314        NA      61167
6  31185             York County, Nebraska     14164        NA      66337
7  08037            Eagle County, Colorado     55693        NA      91338
8  42129 Westmoreland County, Pennsylvania    355107        NA      64708
9  40079         Le Flore County, Oklahoma     48436        NA      43049
10 48053              Burnet County, Texas     48424        NA      65363
   medincomeM medageE medageM                       geometry
1        2489    25.5     0.1 MULTIPOLYGON (((-96.96095 3...
2        5058    44.3     1.0 MULTIPOLYGON (((-94.47167 4...
3        4261    50.7     0.9 MULTIPOLYGON (((-109.7987 4...
4        7039    38.9     1.1 MULTIPOLYGON (((-111.6345 4...
5        2352    46.5     0.5 MULTIPOLYGON (((-92.08384 4...
6        4128    39.5     1.2 MULTIPOLYGON (((-97.82629 4...
7        4058    37.8     0.8 MULTIPOLYGON (((-107.1137 3...
8        1350    47.1     0.2 MULTIPOLYGON (((-79.90487 4...
9        1869    38.8     0.3 MULTIPOLYGON (((-95.05996 3...
10       4694    44.7     0.3 MULTIPOLYGON (((-98.45924 3...
#remove MOE columns - they all end with "M"
mi_counties_withgeo <- mi_counties_withgeo %>%
  select(-ends_with("M"))

mi_counties_withgeo
Simple feature collection with 83 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -90.41814 ymin: 41.69612 xmax: -82.41347 ymax: 48.2388
Geodetic CRS:  NAD83
First 10 features:
   GEOID                            NAME totalpopE medincomeE medageE
1  26119    Montmorency County, Michigan      9164      43913    56.4
2  26147      St. Clair County, Michigan    160067      62847    44.1
3  26053        Gogebic County, Michigan     14858      41948    50.2
4  26047          Emmet County, Michigan     33946      63488    45.6
5  26103      Marquette County, Michigan     66380      57981    39.2
6  26055 Grand Traverse County, Michigan     94562      69393    42.9
7  26141   Presque Isle County, Michigan     12976      52181    55.8
8  26137         Otsego County, Michigan     25016      59330    44.6
9  26003          Alger County, Michigan      8911      48822    49.3
10 26115         Monroe County, Michigan    154068      68006    42.8
                         geometry
1  MULTIPOLYGON (((-84.37166 4...
2  MULTIPOLYGON (((-82.99626 4...
3  MULTIPOLYGON (((-90.41814 4...
4  MULTIPOLYGON (((-85.03905 4...
5  MULTIPOLYGON (((-87.40383 4...
6  MULTIPOLYGON (((-85.58321 4...
7  MULTIPOLYGON (((-84.25202 4...
8  MULTIPOLYGON (((-84.85745 4...
9  MULTIPOLYGON (((-86.70904 4...
10 MULTIPOLYGON (((-83.77392 4...
#remove that trailing "E"
colnames(mi_counties_withgeo) <- sub("E$", "", colnames(mi_counties_withgeo)) # $ means end of string only

mi_counties_withgeo
Simple feature collection with 83 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -90.41814 ymin: 41.69612 xmax: -82.41347 ymax: 48.2388
Geodetic CRS:  NAD83
First 10 features:
   GEOID                             NAM totalpop medincome medage
1  26119    Montmorency County, Michigan     9164     43913   56.4
2  26147      St. Clair County, Michigan   160067     62847   44.1
3  26053        Gogebic County, Michigan    14858     41948   50.2
4  26047          Emmet County, Michigan    33946     63488   45.6
5  26103      Marquette County, Michigan    66380     57981   39.2
6  26055 Grand Traverse County, Michigan    94562     69393   42.9
7  26141   Presque Isle County, Michigan    12976     52181   55.8
8  26137         Otsego County, Michigan    25016     59330   44.6
9  26003          Alger County, Michigan     8911     48822   49.3
10 26115         Monroe County, Michigan   154068     68006   42.8
                         geometry
1  MULTIPOLYGON (((-84.37166 4...
2  MULTIPOLYGON (((-82.99626 4...
3  MULTIPOLYGON (((-90.41814 4...
4  MULTIPOLYGON (((-85.03905 4...
5  MULTIPOLYGON (((-87.40383 4...
6  MULTIPOLYGON (((-85.58321 4...
7  MULTIPOLYGON (((-84.25202 4...
8  MULTIPOLYGON (((-84.85745 4...
9  MULTIPOLYGON (((-86.70904 4...
10 MULTIPOLYGON (((-83.77392 4...

Mapping Michigan counties with mapview

mapview(mi_counties_withgeo, zcol = "medincome")

Customize colors

mapview(mi_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (82)! 
Interpolating color vector to match number of zcol values.

This map’s dark background appeared automatically, because mapview determined the map included a lot of light colors. You can turn off that feature.

mapviewOptions("basemaps.color.shuffle" = FALSE)
mapview(mi_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (82)! 
Interpolating color vector to match number of zcol values.

Two maps together

map_income <- mapview(mi_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (82)! 
Interpolating color vector to match number of zcol values.
map_age <- mapview(mi_counties_withgeo, zcol = "medage", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (74)! 
Interpolating color vector to match number of zcol values.
sync(map_income, map_age)

Side-by-side slider to compare the two, from the leaflet.extras2 package

map_income | map_age

To turn off legends, hover text, popups

mapview(mi_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         legend = FALSE, 
         label = FALSE, 
         popup = FALSE)
Warning: Found less unique colors (9) than unique zcol values (82)! 
Interpolating color vector to match number of zcol values.

Customize labels

mylabel <- glue::glue("{mi_counties_withgeo$NAM} {mi_counties_withgeo$medincome}")


mapview(mi_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         label = mylabel)
Warning: Found less unique colors (9) than unique zcol values (82)! 
Interpolating color vector to match number of zcol values.

Customize popups

mypopup <- glue::glue("<strong>{mi_counties_withgeo$NAM}</strong><br />
                      Total Population: {mi_counties_withgeo$totalpop}<br />
                      Median Income: {mi_counties_withgeo$medincome}") %>% 
  lapply(htmltools::HTML)

mylabel <- glue::glue("{mi_counties_withgeo$State} {mi_counties_withgeo$PctChange10_20}%") %>%
  lapply(htmltools::HTML)
head(mypopup)
[[1]]
<strong>Montmorency County, Michigan</strong><br />
Total Population: 9164<br />
Median Income: 43913

[[2]]
<strong>St. Clair County, Michigan</strong><br />
Total Population: 160067<br />
Median Income: 62847

[[3]]
<strong>Gogebic County, Michigan</strong><br />
Total Population: 14858<br />
Median Income: 41948

[[4]]
<strong>Emmet County, Michigan</strong><br />
Total Population: 33946<br />
Median Income: 63488

[[5]]
<strong>Marquette County, Michigan</strong><br />
Total Population: 66380<br />
Median Income: 57981

[[6]]
<strong>Grand Traverse County, Michigan</strong><br />
Total Population: 94562<br />
Median Income: 69393
mapview(mi_counties_withgeo, zcol = "medincome", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         popup = mypopup)
Warning: Found less unique colors (9) than unique zcol values (82)! 
Interpolating color vector to match number of zcol values.